package goleetcode

func maximumLength(nums []int, k int) int {
	l := len(nums)
	dp := make([][]int, l)
	for i := range dp {
		dp[i] = make([]int, 51)
		for j := range dp[i] {
			dp[i][j] = -1
		}
	}

	ans := 0

	for i := 0; i < l; i++ {
		dp[i][0] = 1
		for j := 0; j <= k; j++ {
			for a := 0; a < i; a++ {
				add := 0
				if nums[i] != nums[a] {
					add = 1
				}
				if j - add >= 0 && dp[a][j - add] != -1 {
					dp[i][j] = max(dp[i][j], dp[a][j - add] + 1)
				}
			}
			ans = max(ans, dp[i][j])
		}
	}

	return ans
}